import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class restrictToInterface {


    public static <I> I restrictToInterface(final I instance, Class<I> publicInterface) {
        Object proxy = Proxy.newProxyInstance(
                publicInterface.getClassLoader(),
                new Class<?>[] {publicInterface},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        return method.invoke(instance, args);
                    }
                }
        );
        return publicInterface.cast(proxy);
    }

//    public static void main(String[] args) {
//        MongoLogHelperImpl beanImpl = new MongoLogHelperImpl();
//        MongoLogHelper remotableBean = restrictToInterface(beanImpl, MongoLogHelper.class);
//        System.out.println("Remoteable Methods are:");
//        for (Method m : remotableBean.getClass().getMethods()) {
//            if (!Modifier.isStatic(m.getModifiers())) {
//                System.out.println("\t" + m.getName());
//            }
//        }
//        remotableBean.slowAddAndLog(1L,"123");
////        remotableBean.foo("Hello world!");
//    }
}
